package com.lianniu.web.baseService.user.controller;

import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.alibaba.fastjson.JSON;
import com.lianniu.web.base.BaseResult;
import com.lianniu.web.baseService.user.model.BuyerinfoVo;
import com.lianniu.web.baseService.user.model.Supply;
import com.lianniu.web.baseService.user.service.IBuyerService;
import com.lianniu.web.baseService.user.service.ISupplyService;
import com.lianniu.web.product.model.SupplyareaVo;
import com.lianniu.web.product.service.ISupplyareaService;

@Controller
@RequestMapping("/rest/supply")
public class SupplyRestController {
    private final static Logger logger = Logger.getLogger(SupplyRestController.class);
    @Autowired
    ISupplyService supplyService;
    @Autowired
    IBuyerService buyerService;
    @Autowired
    ISupplyareaService supplyareaService;

    @RequestMapping(value = {"/supplyEdit"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult supplyEdit(HttpServletRequest request, Supply supply) {
        logger.info("SupplyRestController SupplyEdit begin.");
        logger.info("SupplyRestController SupplyEdit:" + JSON.toJSONString(supply));
        try {
            logger.info("SupplyEdit begin.");
            String identyCardPath = request.getParameter("identyCardPath");
            String lawPath = request.getParameter("lawPath");
            supply.setLawPath(lawPath);
            supply.setIdentyCardPath(identyCardPath);
            BaseResult baseResult = supplyService.updateId(supply);
            if (supply.getIdentyCardPath() != null) {
                baseResult.setData(identyCardPath);
            }
            if (supply.getLawPath() != null) {
                baseResult.setData(lawPath);
            }
            logger.info("SupplyEdit baseResult:" + JSON.toJSONString(baseResult));
            logger.info("SupplyEdit end.");
            return baseResult;
        } catch (Exception e) {
            logger.error("SupplyEdit error:" + e.getMessage());
            logger.info("SupplyRestController SupplyEdit end.");
            return BaseResult.exception(e.getMessage());
        }
    }

    @RequestMapping(value = {"/getSupply"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult getSupply(HttpServletRequest request) {
        String userId = request.getParameter("userid");
        logger.info(String.format("get %s supply", userId));
        BaseResult supply = supplyService.getSupplyId(userId);
        logger.info("get supply result : " + JSON.toJSONString(supply));
        return supply;
    }

    @RequestMapping(value = {"/supply"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult supply(HttpServletRequest request) {
        String userId = request.getParameter("userid");
        logger.info(String.format("get %s supply", userId));
        BaseResult supply = supplyService.getSupplyId(userId);
        logger.info("get supply result : " + JSON.toJSONString(supply));
        return supply;
    }

    @RequestMapping(value = {"/addSupply"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult addSupply(HttpServletRequest request, Supply supply) {
        logger.info("SupplyRestController addSupply begin.");
        logger.info("SupplyRestController addSupply:" + JSON.toJSONString(supply));
        String userId = request.getParameter("userId");
        BaseResult baseResult = null;
        baseResult = supplyService.selectByUsersId(userId);
        Supply s = (Supply) baseResult.getData();
        String supplyId = "";
        try {
            if (s == null) {
            	baseResult = buyerService.selectBuyerById(userId);
            	BuyerinfoVo buyer = (BuyerinfoVo) baseResult.getData();
            	if(buyer != null){
            		 Supply su = new Supply();
            		 su.setUserId(buyer.getUserId());
            		 su.setSupplyName(buyer.getCompName());
            		 su.setProvince(buyer.getProvince());
            		 su.setCity(buyer.getCity());
            		 su.setCounty(buyer.getCounty());
            		 su.setAddress(buyer.getAddress());
            		 su.setDelFlag("1");
            		 baseResult = supplyService.addSupply(su);
            		 supplyId = String.valueOf(su.getSupplyId());
            		 SupplyareaVo supplyarea = new SupplyareaVo();
            		 //supplyarea.setID(StringUtil.getUUID());
            		 supplyarea.setSupplyID(supplyId);
            		 supplyarea.setDelFlag("1");
            		 supplyarea.setAreaID(buyer.getCounty()+",");
            		 supplyarea.setAddUser(buyer.getBuyerName());
            		 supplyareaService.insertSupplyarea(supplyarea);
            	}
            	logger.info("addSupply baseResult:" + JSON.toJSONString(baseResult));
                logger.info("addSupply end.");
               return BaseResult.success(supplyId);
              // return BaseResult.success(s);
            } else {
                logger.info("addSupply baseResult:" + JSON.toJSONString(baseResult));
                logger.info("addSupply end.");
                return BaseResult.success(s.getSupplyId());
                //return BaseResult.success(s);
            }
        } catch (Exception e) {
            logger.info("addSupply exception:" + JSON.toJSONString(e.getMessage()));
            logger.info("addSupply end.");
            return BaseResult.exception(e.getMessage());
        }
        
    }
    @RequestMapping(value = {"/selectByUsersId"}, method = RequestMethod.POST)
    public @ResponseBody BaseResult selectByUsersId(HttpServletRequest request,String userId){
        logger.info("selectByUsersId bedin");
        BaseResult baseResult = supplyService.selectByUsersId(userId);
        logger.info("selectByUsersId end");
        return baseResult;
    }
    /**
     * 
     * @Title: updateSupplyByUserId 
     * @Description: 跟新卖家开通在线支付状态
     * @author: guoconglin
     * @param request
     * @param userId
     * @return
     * @date: 2016年10月13日 上午11:09:20 
     * @return: BaseResult
     */
    @RequestMapping(value = {"/updateSupplyByUserId"}, method = {RequestMethod.GET,RequestMethod.POST})
    public @ResponseBody BaseResult updateSupplyByUserId(HttpServletRequest request,String userId){
        logger.info("updateSupplyByUserId begin");
        BaseResult baseResult = supplyService.updateSupplyByUserId(userId);
        logger.info("updateSupplyByUserId end");
        return baseResult;
    }
}
